package com.ruoyi.project.cms.content.controller;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.content.domain.CmsContent;
import com.ruoyi.project.cms.content.domain.CmsTypeVo;
import com.ruoyi.project.cms.content.domain.ContentVo;
import com.ruoyi.project.cms.content.service.IContentService;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.cms.type.service.ICmsTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-16 09:17
 * @description: 公告内容管理的控制器
 **/
@Controller
@RequestMapping("/cms/content")
public class ContentController extends BaseController {

    @Autowired
    private IContentService contentService;

    @Autowired
    private ICmsTypeService cmsTypeService;
    //如果是使用tempmeleaf,前面不能加"/"
    private String prefix = "cms/content";


    /**
     * 创建显示内容管理主页的控制器单元方法
     *
     * @return 公告内容主页的地址
     */
    @RequiresPermissions("cms:content:view")
    @GetMapping()
    public String content() {
        return prefix + "/content";
    }

    /**
     * 创建显示内容管理主页的控制器单元方法
     *
     * @return 公告内容主页的地址
     */

    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable Long contentId, ModelMap modelMap) {
        //根据传递的id查询content数据,并将对象放到作用域中
        CmsContent Content = contentService.selectContentById(contentId);
        modelMap.addAttribute("content", Content);
        //还需将所有的cmsType对象传递到页面中
        //调用service查询数据
        CmsType cmsType = new CmsType();
        List<CmsType> cmsTypes = cmsTypeService.selectCmsTypeList(cmsType);
        List<CmsTypeVo> cmsTypeVos = new ArrayList<>();
        for (CmsType type : cmsTypes) {
            CmsTypeVo cmsTypeVo = new CmsTypeVo();
            BeanUtils.copyBeanProp(cmsTypeVo, type);
            cmsTypeVos.add(cmsTypeVo);

        }
        modelMap.addAttribute("vos", cmsTypeVos);


        return prefix + "/edit";
    }

    /**
     * 跳转到新增公告的地址
     *
     * @return
     */
    @RequiresPermissions("cms:content:add")
    @GetMapping("/add")
    public String add(Model model) {
        CmsType cmsType = new CmsType();
        //调用service查询数据
        List<CmsType> cmsTypes = cmsTypeService.selectCmsTypeList(cmsType);
        List<CmsTypeVo> cmsTypeVos = new ArrayList<>();
        for (CmsType type : cmsTypes) {
            CmsTypeVo cmsTypeVo = new CmsTypeVo();
            BeanUtils.copyBeanProp(cmsTypeVo, type);
            cmsTypeVos.add(cmsTypeVo);

        }
        model.addAttribute("vos", cmsTypeVos);
        return prefix + "/add";
    }


    /**
     * 添加公告:需要给定权限(cms:content:add)
     * 是post请求
     *
     * @param cmsContent
     * @return 返回的是插入结果
     */
    @RequiresPermissions(("cms:content:add"))
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsContent cmsContent) {
        return toAjax(contentService.insertContent(cmsContent));
    }

    /**
     * 内容的分页查询和条件查询
     * <p>
     * 1.使用pageHelper进行分页
     * 2.调用service数据
     *
     * @param cmsContent
     * @return
     */
    @RequiresPermissions("cms:view:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsContent cmsContent) {
        startPage();
        List<ContentVo> list = contentService.selectContentList(cmsContent);
        return getDataTable(list);
    }

    /**
     * 根据id指定的公告内容,可批量删除
     *
     * @param ids
     * @return
     */

    @RequiresPermissions("cms:view:remove")
    @ResponseBody
    @PostMapping("/remove")
    public AjaxResult remove(Long[] ids) {
        return toAjax(contentService.deleteContentByIds(ids));

    }





}
